package com.note.chh.notebook.UI.enjoy;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.note.chh.notebook.Adapter.MusicAdapter;
import com.note.chh.notebook.R;
import com.note.chh.notebook.UI.base.BaseActivity;
import com.note.chh.notebook.Uitls.ItemDivider;
import com.note.chh.notebook.Uitls.localFile.FileManager;
import com.note.chh.notebook.Uitls.localFile.bean.AudioItem;
import com.qmuiteam.qmui.widget.QMUITopBar;

import java.io.Serializable;
import java.util.ArrayList;

import butterknife.BindView;

//音乐
public class MusicActivity extends BaseActivity {

    @BindView(R.id.topbar)
    QMUITopBar topbar;
    @BindView(R.id.mRecycle_view)
    RecyclerView mRecycleView;
    private BaseQuickAdapter musicAdapter;
    //本地音乐列表
    public static ArrayList<AudioItem> musics;

    @Override
    public void initTopBar(){
        super.initTopBar();
        topbar.setBackgroundColor(getResources().getColor(R.color.app_color_blue));
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        topbar.setTitle(getResources().getString(R.string.music));
    }

    @Override
    public void initView(){
        mRecycleView.setLayoutManager(new LinearLayoutManager(this));
        //设置条目分割线
        mRecycleView.addItemDecoration(new ItemDivider().setDividerWith(1).setDividerColor(getResources().getColor(R.color.color_999999)));
        //如果Item高度固定  增加该属性能够提高效率
        mRecycleView.setHasFixedSize(true);
        musicAdapter = new MusicAdapter(R.layout.item_music_list);
        //设置加载动画
        musicAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        mRecycleView.setAdapter(musicAdapter);
    }

    @Override
    public void initData() {
        //获取本地的音乐列表
        musics = FileManager.getInstance().getMusics(context);
        musicAdapter.setNewData(musics);



    }

    @Override
    public void initListener() {
        super.initListener();
        musicAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                AudioItem item = (AudioItem) adapter.getItem(position);
                Bundle bundle = new Bundle();
                bundle.putString("title",item.getName());
                bundle.putString("musicUrl",item.getPath());
                bundle.putInt("position",position);
                //序列化,要注意转化(Serializable)
                bundle.putSerializable("musicList",(Serializable) musics);
                startActivity(PlayMusicActivity.class,bundle);
            }
        });
    }

    @Override
    protected int bindLayout() {

        return R.layout.activity_music;
    }

}
